package net.soevar.database.dao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import net.soevar.database.hibernate.HibernateUtil;
import net.soevar.database.model.AthUser;

public class AthUserDaoImpl extends
		GenericHibernateDao<AthUser, Long, AthUserDao> implements AthUserDao {

	@Override
	public Boolean login(AthUser users) {
		Criteria criteria = createCriteria();
		criteria.setProjection(Projections.count("id"));
		criteria.add(Restrictions.eq("userId", users.getUserId()));
		criteria.add(Restrictions.eq("userPassword", users.getUserPassword()));
		return ((Number) criteria.uniqueResult()).intValue() != 0;
	}

	@Override
	public AthUser loadByUsernameAndPassWithNewSession(AthUser users) {
		Session session = HibernateUtil.currentSession();
		Criteria criteria = session.createCriteria(AthUser.class);
		criteria.add(Restrictions.eq("userId", users.getUserId()));
		criteria.add(Restrictions.eq("userPassword", users.getUserPassword()));
		AthUser users2 = ((AthUser) criteria.uniqueResult());
		return users2;
	}

	@Override
	public Boolean loginWithNewSession(AthUser users) {
		Session session = HibernateUtil.currentSession();
		Criteria criteria = session.createCriteria(AthUser.class);
		criteria.setProjection(Projections.count("id"));
		criteria.add(Restrictions.eq("userId", users.getUserId()));
		criteria.add(Restrictions.eq("userPassword", users.getUserPassword()));
		Boolean result = ((Number) criteria.uniqueResult()).intValue() != 0;
		return result;
	}

	@Override
	public AthUser loadByUsernameAndPass(AthUser users) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("userId", users.getUserId()));
		criteria.add(Restrictions.eq("userPassword", users.getUserPassword()));
		return ((AthUser) criteria.uniqueResult());
	}

	@Override
	public Boolean isExist(AthUser users) {
		Criteria criteria = createCriteria();
		criteria.add(Restrictions.eq("userId", users.getUserId()));
		return criteria.setMaxResults(net.soevar.common.Common.MAX_RESULT)
				.list().size() != 0;

	}

}
